package com.example.config;

import com.example.anno.CurrJobSeeker;
import com.example.pojo.entity.JobSeeker;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author 小岁
 * @version 2023/10/1 22:08
 */
public class HandlerArgsConfig implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterAnnotation(CurrJobSeeker.class)!=null && parameter.getParameterType()== JobSeeker.class;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
        //将这个request作用域中的数据,绑定到@CurrUser这个注解修饰的那个对象上去
        return request.getAttribute("currJobSeeker");
    }
}
